from playwright.sync_api import Playwright, sync_playwright, expect
import re
import time


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context(ignore_https_errors=True, record_har_mode="full", record_har_path="D:\\webrunner\\webrunner-x86_64-windows-20250616145951-16898d5b9\\filestorage\\recorder\\simulate\\测盟会课程\\har_1751699354.har", service_workers="block")
    time.sleep(1)
    page = context.new_page()
    time.sleep(1)
    page.goto("http://localhost:8080/")
    time.sleep(1)
    page.goto("http://localhost:8080/news")
    time.sleep(1)
    page.goto("http://localhost:8080/login")
    time.sleep(1)
    page.get_by_text("记住我登录").click()
    time.sleep(1)
    page.get_by_placeholder("用户名").click()
    time.sleep(1)
    page.get_by_placeholder("用户名").fill("admin")
    time.sleep(1)
    page.get_by_placeholder("密码").click()
    time.sleep(1)
    page.get_by_placeholder("密码").fill("123456")
    time.sleep(1)
    page.get_by_role("button", name="登录").click()
    time.sleep(1)
    page.get_by_role("menuitem", name="课程管理").click()
    time.sleep(1)
    page.get_by_role("button", name="新增课程").click()
    time.sleep(1)
    page.get_by_role("textbox", name="* 课程名称").click()
    time.sleep(1)
    page.get_by_role("textbox", name="* 课程名称").fill("test")
    time.sleep(1)
    page.get_by_placeholder("请输入课程作者").click()
    time.sleep(1)
    page.get_by_placeholder("请输入课程作者").fill("test")
    time.sleep(1)
    page.get_by_placeholder("请输入课程简介").click()
    time.sleep(1)
    page.get_by_placeholder("请输入课程简介").fill("test")
    time.sleep(1)
    page.get_by_role("dialog", name="新增课程").get_by_role("button", name="增加数值").click()
    time.sleep(1)
    page.get_by_role("button", name="选择封面").click()
    time.sleep(1)
    page.get_by_role("button", name="选择封面").set_input_files("tv.jpg")
    time.sleep(1)
    page.get_by_role("button", name="选择视频").click()
    time.sleep(1)
    page.get_by_role("button", name="选择视频").set_input_files("1.mp4")
    time.sleep(1)
    page.get_by_role("button", name="确定").click()
    time.sleep(1)
    page.get_by_role("textbox", name="课程名称").click()
    time.sleep(1)
    page.get_by_role("textbox", name="课程名称").fill("test")
    time.sleep(1)
    page.get_by_role("button", name="查询").click()
    time.sleep(1)
    page.get_by_role("button", name="编辑").click()
    time.sleep(1)
    page.get_by_role("textbox", name="* 课程名称").click()
    time.sleep(1)
    page.get_by_role("textbox", name="* 课程名称").fill("test1")
    time.sleep(1)
    page.get_by_placeholder("请输入课程简介").click()
    time.sleep(1)
    page.get_by_placeholder("请输入课程简介").click()
    time.sleep(1)
    page.get_by_placeholder("请输入课程简介").fill("test1")
    time.sleep(1)
    page.get_by_role("button", name="确定").click()
    time.sleep(1)
    page.get_by_role("row", name="test1 test test1 1 已审核 查看 编辑 删除").get_by_role("button", name="删除").click()
    time.sleep(1)
    page.get_by_role("button", name="确定").click()
    page.close()

    # ---------------------
    time.sleep(2)
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)
